﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace ShopOnline.Models.DAL
{
    public class DAL_Contact
    {
         private ShopOnline.Models.ShopOnlineDBDataContext SC_DB;
         public DAL_Contact()
        {
            SC_DB = new ShopOnlineDBDataContext();
        }


         public List<Contact> getListContact()
        {
            return SC_DB.Contacts.ToList();
        }


         public Contact getContactById(int contact)
        {
            return (from c in SC_DB.Contacts where c.contactId == contact select c).FirstOrDefault();
        }

         public int CreateContact(Contact contact)
        {
            int result = 0;
            try
            {
                SC_DB.Contacts.InsertOnSubmit(contact);
                SC_DB.SubmitChanges();
                result = 1;
            }
            catch
            {

            }
            return result;
        }

         public int EditContact(Contact con)
        {
            int result = 0;
            try
            {
                var contact = (from c in SC_DB.Contacts where c.contactId == con.contactId select c).FirstOrDefault();
                contact.abbrev = con.abbrev;
                contact.address = con.address;
                contact.companyName = contact.companyName;
                contact.email = contact.email;
                contact.fax = contact.fax;
                contact.phone = contact.phone;
                contact.website = contact.website;
                SC_DB.SubmitChanges();
                result = 1;
            }
            catch
            {

            }
            return result;
        }
         public int DeleteContact(Contact con)
        {
            int result = 0;
            try
            {
                var pic = (from c in SC_DB.Contacts where c.contactId == con.contactId select c).FirstOrDefault();
                SC_DB.Contacts.DeleteOnSubmit(con);
                SC_DB.SubmitChanges();
                result = 1;
            }
            catch
            {
                
            }
            return result;
        }

         public Contact getContact()
         {
             Contact contact = new Contact();
             if (getListContact().Count>0)
             {
                 contact = (from c in SC_DB.Contacts select c).FirstOrDefault();
             }
             else
             {
                 contact.companyName = "";
                 contact.abbrev = "";
                 contact.address = "";
                 contact.email = "";
                 contact.fax = "";
                 contact.phone = "";
                 contact.website = "";
             }
             return contact;
         }
    }
}